package ru.papergames.battleserver.network;

import org.mmocore.network.*;
import ru.papergames.battleserver.BattleThreadPoolManager;
import ru.papergames.battleserver.network.clientpackets.webserver.*;

import java.nio.ByteBuffer;

/**
 * @author: atroshanin
 * Date: 18.04.2010
 * Time: 17:00:11
 */
public class WebServerPacketHandler implements
        IPacketHandler<WebServerClient>,
        IClientFactory<WebServerClient>,
        IMMOExecutor<WebServerClient>,
        WebServerClientPacketTypes {

    public WebServerClientPacket handlePacket(ByteBuffer buf, WebServerClient client) {
        if (client.isDetached())
            return null;

        int packetId = buf.get() & 0xFF;

        WebServerClientPacket msg = null;
        switch (client.getState()) {
            case CONNECTED: {
                switch (packetId) {
                    case AUTHORIZE_WEB_SERVER_PACKET_ID:
                        msg = new AuthorizeWebServer();
                        break;
                }
                break;
            }
            case AUTHORIZED: {
                switch (packetId) {
                    case ADD_NPC_PLAYERS_PACKET_ID:
                        msg = new AddNewNpcPlayers();
                        break;
                    case ADD_NEW_PC_PLAYER_PACKET_ID:
                        msg = new AddNewPcPlayer();
                        break;
                    case CREATE_NEW_BATTLE_PACKET_ID:
                        msg = new CreateNewBattle();
                        break;
/*
                    case JOIN_EXISTING_BATTLE_REQUEST_PACKET_ID:
                        msg = new JoinOsidePcPlayerToExistingBattleRequest();
                        break;
*/
                }
                break;
            }
        }
        return msg;
    }

    public WebServerClient create(MMOConnection<WebServerClient> con) {
        return new WebServerClient(con);
    }

    public void execute(ReceivablePacket<WebServerClient> rp) {
        BattleThreadPoolManager.getInstance().executeWSPacket(rp);
    }
}
